﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace zielonka.co.uk.extensions.system.io
{
    // Description:	Returns a string with the content of the input stream
    /*
        //This is only an example. A file could be easily read to a String by using "System.IO.File.ReadAllText"

        using (System.IO.FileStream fs = new System.IO.FileStream(@"c:\test.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read))
        {
            String StreamContent = fs.ReadToEnd();
        }
     */

    public static partial class StreamExtensions
    {
        private const int DefaultBufferSize = 0x1000;

        /// <summary>
        /// Writes the entire contents of this stream to another stream using a buffer.
        /// </summary>
        /// <param name="sourceStream">The source stream.</param>
        /// <param name="stream">The stream to write this stream to.</param>
        /// <param name="bufferSize">Size of the buffer.</param>
        /// <remarks>
        /// The size of the buffer is 4096 bytes
        /// </remarks>
        public static void WriteTo(this Stream sourceStream, Stream stream)
        {
            WriteTo(sourceStream, stream, DefaultBufferSize);
        }

        /// <summary>
        /// Writes the entire contents of this stream to another stream using a buffer
        /// with the specified size.
        /// </summary>
        /// <param name="sourceStream">The source stream.</param>
        /// <param name="stream">The stream to write this stream to.</param>
        /// <param name="bufferSize">Size of the buffer.</param>
        public static void WriteTo(this Stream sourceStream, Stream stream, int bufferSize)
        {
            byte[] buffer = new byte[bufferSize];
            int n;
            while ((n = sourceStream.Read(buffer, 0, buffer.Length)) != 0)
                stream.Write(buffer, 0, n);
        }
    }
}
